package actionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import util.DomainUtils;

public class ActionGroupDispatcherValidationForm  extends ActionForm{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String current_groups_versions = null;
	private String selected_contactgroup = null;
	private String wanted_action = null;
	private String new_group_name = null;



	public String getCurrent_groups_versions() {
		return current_groups_versions;
	}
	public void setCurrent_groups_versions(String current_groups_versions) {
		this.current_groups_versions = current_groups_versions;
	}

	public String getSelected_contactgroup() {
		return selected_contactgroup;
	}
	public void setSelected_contactgroup(String selected_contactgroup) {
		this.selected_contactgroup = selected_contactgroup;
	}
	public String getWanted_action() {
		return wanted_action;
	}
	public void setWanted_action(String wanted_action) {
		this.wanted_action = wanted_action;
	}
	public String getNew_group_name() {
		return new_group_name;
	}
	public void setNew_group_name(String new_group_name) {
		this.new_group_name = DomainUtils.trim(new_group_name);
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.selected_contactgroup  = null;
		this.wanted_action = null;
		this.new_group_name = null;
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession(false);

		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			errors.add(
					DomainUtils.ALERT_ERROR_STRING,
					new ActionMessage(
							"session.error"));

			DomainUtils.keepParametersContent(request);

			return errors;
		}

		if(getSelected_contactgroup() == null || getSelected_contactgroup().isEmpty()){
			errors.add("selected_contactgroup", new ActionMessage(
					"actiongroupdispatcher.selected_contactgroup.error"));

			DomainUtils.keepParametersContent(request);

			return errors;
		}
		
		//checking action
		if(getWanted_action() == null || getWanted_action().isEmpty()){
			errors.add("wanted_action", new ActionMessage(
					"actiongroupdispatcher.wanted_action.error"));

			DomainUtils.keepParametersContent(request);

			return errors;
		}	

		if(getWanted_action().compareTo("modify_contactgroup") == 0){

			String group_name = getNew_group_name();

			//Checking group name
			if(group_name == null || group_name.isEmpty())
			{
				errors.add("group_name", new ActionMessage(
						"modifycontactgroup.group_name.error"));

				DomainUtils.keepParametersContent(request);

				return errors;
			}

			if(DomainUtils.startByALetter(group_name) == false && 
					DomainUtils.startByUnderscore(group_name) == false) {

				errors.add("group_name", new ActionMessage(
						"modifycontactgroup.group_name.startbyletterorunderscore.error"));

				DomainUtils.keepParametersContent(request);

				return errors;
			}

			if(DomainUtils.containsALetter(group_name) == false) {
				errors.add("group_name", new ActionMessage(
						"modifycontactgroup.group_name.containoneletter.error"));

				DomainUtils.keepParametersContent(request);

				return errors;
			}

			if(DomainUtils.match(group_name, "\\w+") == false){

				errors.add("group_name", new ActionMessage(
						"modifycontactgroup.group_name.containnostrangecharacter.error"));

				DomainUtils.keepParametersContent(request);

				return errors;
			}
		}
		return errors;
	}
}
